extends layout
block content
  h3 encore testnet ether faucet
  div.div.panel.panel-default
    div.div.panel-heading
      h4 Faucet
    div.div.panel-body
      p address: #{data.faucetAddress}
      p balance: #{ethformatter(data.faucetBalance)}
      form(action='faucet' method='post')
        .form-group
          //input.from-control#useraccount(type='hidden', name="useraccount")
          input.from-control#useraccount(type='text', placeholder='User Account', name="useraccount")
        |
        button.btn.btn-success(type='submit') request 1 ether from faucet
  div.div.panel.panel-default
    div.div.panel-heading
      h4 Transactions
    div.div.panel-body
      p#transaction
        a(href="tx/" + data.transaction) #{data.transaction}
        p(style="color: red") #{data.errorMessage}
  script.
    var receipt = null;
    function getAccount() {
      var account = metamask.eth.accounts[0];
      if (account) {
        document.getElementById("user-address").innerHTML = 'address: ' + account;
        document.getElementById("useraccount").value = account;
        return account;
      }
      return null;
    }
    function getBalance() {
      var account = metamask.eth.accounts[0];
      if (account) {
         web3.eth.getBalance(account, function(err, balance) {
           document.getElementById("user-balance").innerHTML = 'balance: ' + metamask.fromWei(balance);
         });
      }
    }
    function getEther() {
       var account = getAccount();
       if (!account) return alert('no user accounts found!');
    }
    function showTransactionDiv() {
      var tx  = !{JSON.stringify(data.transaction)};
      var err = !{JSON.stringify(data.errorMessage)};
      if (tx || err) {
        window.location.hash = "#transaction";
      }
    }
    async function enableEthereum() {
      if (window.ethereum) {
        metamask = new Web3(window.ethereum);
        try {
          await window.ethereum.enable();
        } catch (error) {
          console.log(error);
        }
      }
    }
    window.addEventListener('load', async () => {
      await enableEthereum();
      validateChainId(!{JSON.stringify(data.chainId)}, !{JSON.stringify(data.rpcUrlSuggestion)});
      showTransactionDiv();
      getAccount();
      getBalance();
    })
